<?php
namespace app\api\controller\wsgoods;
class Useraddress extends Base{

    public function __construct() {
        parent::__construct();
        $this->checkLogin();
    }

    //用户地址
    public function lists(){
        //收货地址
        $field = [
            'id',
            'user_id',
            'address_name',
            'address_phone',
            'province',
            'city',
            'district',
            'address',
            'is_default',
        ];
        $field = implode(",", $field);
        $addrlist = $this->db->query("select {$field} from user_address where user_id=? order by id desc",[$this->userinfo["id"]]);
        suc($addrlist);
    }

    //添加地址
    public function add(){
        $d = $this->post();
        $rule = [
            'addressname'=>'require',
            'addresstel'=>'require',
            'province'=>'require',
            'city'=>'require',
            'district'=>'require',
            'addressmx'=>'require',
        ];
        $msg = [
            'addressname.require' => '请输入姓名',
            'addresstel.require' => '请输入电话',
            'province.require' => '请选择省',
            'city.require' => '请选择市区',
            'district.require' => '请选择地区',
            'addressmx.require' => '请填写详细地址',
        ];
        check($rule,$msg,$d);
        $default = $this->post("is_default");
        $rs = $this->db->query("select * from user_address where user_id=?",[$this->userinfo["id"]]);
        if(!$rs){
            $default = 1;
        }else{
            if($default == 1){
                //修改其他默认
                $this->db->update("user_address",['is_default'=>0],["user_id=?",[$this->userinfo["id"]]]);
            }
        }
        $data = [
            "user_id" => $this->userinfo["id"],
            "address_name" => $d['addressname'],
            "province" => $d['province'],
            "city" => $d['city'],
            "district" => $d['district'],
            "address" => $d['addressmx'],
            "address_phone" => $d['addresstel'],
            'is_default' => $default,
            "create_time" => time(),
            "update_time" => time()
        ];
        $re = $this->db->insert("user_address",$data);
        if($re){
            return suc('保存成功!');
        }else{
            err('保存错误!');
        }
    }

    //编辑地址详情
    public function details(){
        $id = $this->post("id");
        //所有省
        $plist = $this->db->query('select id,name from com_address where parent_id=? and type=1',[1]);
        $field = [
            'id',
            'user_id',
            'address_name',
            'address_phone',
            'province',
            'city',
            'district',
            'address',
            'is_default',
        ];
        $field = implode(",", $field);
        $ainfo = $this->db->query("select {$field} from user_address where id=? and user_id=?",[$id,$this->userinfo["id"]],1);
        $d = [];
        if($ainfo){
            foreach ($plist as $k => $v) {
                if($v['name'] == $ainfo['province']){
                    $ainfo['province_id'] = $v['id'];
                }
            }
            $clist = $this->db->query('select id,name from com_address where name=? and type=2',[$ainfo["city"]]);
            foreach ($clist as $k => $v) {
                if($v['name'] == $ainfo['city']){
                    $ainfo['city_id'] = $v['id'];
                }
            }

            $dlist = $this->db->query('select id,name from com_address where name=? and type=3',[$ainfo["district"]]);
            foreach ($dlist as $k => $v) {
                if($v['name'] == $ainfo['district']){
                    $ainfo['district_id'] = $v['id'];
                }
            }
        }
        $d['addrinfo'] = $ainfo;
        $d['provincelist'] = $plist;
        suc($d);
    }

    //编辑地址
    public function edit(){
        $d = $this->post();
        $rule = [
            'addressname'=>'require',
            'addresstel'=>'require',
            'province'=>'require',
            'city'=>'require',
            'district'=>'require',
            'addressmx'=>'require',
        ];
        $msg = [
            'addressname.require' => '请输入姓名',
            'addresstel.require' => '请输入电话',
            'province.require' => '请选择省',
            'city.require' => '请选择市区',
            'district.require' => '请选择地区',
            'addressmx.require' => '请填写详细地址',
        ];
        check($rule,$msg,$d);
        $default = $this->post("is_default");
        $data = [
            "user_id" => $this->userinfo["id"],
            "address_name" => $d['addressname'],
            "province" => $d['province'],
            "city" => $d['city'],
            "district" => $d['district'],
            "address" => $d['addressmx'],
            "address_phone" => $d['addresstel'],
            'is_default' => (int)$default,
            "create_time" => time(),
            "update_time" => time()
        ];
        if($default == 1){
            //修改其他默认
            $this->db->update("user_address",['is_default'=>0],["user_id=?",[$this->userinfo["id"]]]);
        }
        $rs = $this->db->update("user_address",$data,["id=?",[$d['addressid']]]);
        if($rs){
            return suc('保存成功!');
        }else{
            err('保存错误!');
        }
    }

    //删除地址
    public function del(){
        $id = $this->post("id");
        $info = $this->db->query('select * from user_address where id=? and user_id=?',[$id,$this->userinfo["id"]]);
        if($info){
            $re = $this->db->delete('user_address',$id,'id');
            if($re){
                return suc('删除成功!');
            }else{
                err('删除失败!');
            }
        }else{
            err('删除失败!');
        }
    }

    //设置默认
    public function setDefault(){
        $id = $this->post("id");
        $info = $this->db->query('select * from user_address where id=? and user_id=?',[$id,$this->userinfo["id"]]);
        if($info){
            //修改其他默认
            $this->db->update("user_address",['is_default'=>0],["user_id=?",[$this->userinfo["id"]]]);
            $data = [
                'is_default' => 1
            ];
            $rs = $this->db->update("user_address",$data,["id=?",[$id]]);
            if($rs){
                suc('设置成功!');
            }else{
                err('设置失败!');
            }
        }else{
            err('设置失败!');
        }
    }

    //获取用户默认地址
    public function getDefault(){
        //用户地址信息获取
        $map = [];
        $map[] = ["and","user_id","=",$this->userinfo["id"]];
        $w = where($map);
        $field = [
            'id',
            'user_id',
            'address_name',
            'address_phone',
            'province',
            'city',
            'district',
            'address',
            'is_default',
        ];
        $field = implode(",", $field);
        $address_info = $this->db->query("select {$field} from user_address  $w[0] order by is_default desc",$w[1],1);
        suc($address_info);
    }
}
?>
